package com.wechat.page.chat.message.msg;

import com.swak.fx.support.ability.DragAbleAbility;
import com.swak.fx.support.ui.UIImage;
import com.swak.fx.support.ui.UILifecycle;
import com.swak.utils.FileUtils;
import com.wechat.dto.MessageDTO;
import com.wechat.msg.FileMsg;
import com.wechat.page.Medias;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;

/**
 * 文件消息
 * 
 * @author 618lf
 */
public class FileMsgComp extends AbstractMsgComp implements MsgComp, UILifecycle {

	private final String STYLE_CLASS = "message__file";
	private final String ARROWS_STYLE_CLASS = "message__file-arrows";
	private final String INNER_STYLE_CLASS = "message__file-inner";
	private final String MAIN_STYLE_CLASS = "message__file-main";
	private final String NAME_STYLE_CLASS = "message__file-name";
	private final String SIZE_STYLE_CLASS = "message__file-size";
	private final String STATUS_STYLE_CLASS = "message__file-status";

	private final VBox wrap;
	private final GridPane inner;
	private final TextFlow nameFlow;
	private final Text name;
	private final Text size;
	private final UIImage image;
	private final Pane placeholder;
	private final TextFlow status;

	public FileMsgComp(MessageDTO message) {
		super(message);
		this.wrap = new VBox();
		this.inner = new GridPane();
		this.nameFlow = new TextFlow();
		this.name = new Text();
		this.size = new Text();
		this.image = new UIImage();
		this.placeholder = new Pane();
		this.status = new TextFlow();
		this.init();
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.getStyleClass().add(STYLE_CLASS);
		this.arrows.getStyleClass().add(ARROWS_STYLE_CLASS);
		this.wrap.getStyleClass().add(INNER_STYLE_CLASS);
		this.inner.getStyleClass().add(MAIN_STYLE_CLASS);
		this.nameFlow.getStyleClass().add(NAME_STYLE_CLASS);
		this.size.getStyleClass().add(SIZE_STYLE_CLASS);
		this.status.getStyleClass().add(STATUS_STYLE_CLASS);

		this.inner.setPrefWidth(220);
		this.inner.setMinHeight(100);
		this.setMaxWidth(Max_Width);
		this.image.setPrefSize(45, 45);
		this.status.setPrefSize(100, 25);
		this.nameFlow.setTextAlignment(TextAlignment.JUSTIFY);

		this.wrap.getChildren().add(this.inner);
		this.wrap.getChildren().add(this.status);

		this.nameFlow.getChildren().add(this.name);
		this.inner.getChildren().add(this.nameFlow);
		this.inner.getChildren().add(this.size);
		this.inner.getChildren().add(this.image);
		this.inner.getChildren().add(this.placeholder);

		GridPane.setVgrow(placeholder, Priority.ALWAYS);
		GridPane.setHgrow(nameFlow, Priority.ALWAYS);
		GridPane.setValignment(image, VPos.TOP);
		GridPane.setMargin(size, new Insets(10, 0, 0, 0));
		GridPane.setMargin(image, new Insets(0, 0, 0, 5));
		GridPane.setConstraints(nameFlow, 0, 0);
		GridPane.setConstraints(size, 0, 1);
		GridPane.setConstraints(placeholder, 0, 2);
		GridPane.setConstraints(image, 1, 0);
		GridPane.setRowSpan(image, 2);
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.prefHeightProperty().bind(this.inner.heightProperty());
		this.fileProperty.addListener((ob, o, n) -> {
			this.name.setText(n.getName());
			this.size.setText(FileUtils.formatFileSize(n.getSize()));
			Medias media = Medias.of(n.getName());
			this.image.Direct(media.images[2]);
		});
	}

	@Override
	public void initializeFinish() {
		super.initializeFinish();
		FileMsg msg = FileMsg.fromXml(message.getMsgContent(), FileMsg.class);
		this.fileProperty.set(msg);

		// 添加可移动的能力
		new DragAbleAbility(this.inner, () -> message);
	}

	@Override
	protected Node getContentNode() {
		return this.wrap;
	}

	private final ObjectProperty<FileMsg> fileProperty = new SimpleObjectProperty<>();
}